<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>

  <fmt:setBundle basename="ApplicationMessage" />
  <fmt:setLocale value="zh_CN" />

  <%@ include file="/WEB-INF/include/meta.jsp"%>
  <%@ include file="/WEB-INF/include/css.jsp"%>
  <link href="${ctx.resource}/css/select2.css" rel="stylesheet">
  <link href="${ctx.resource}/css/select2-bootstrap.css" rel="stylesheet">
  <link href="${ctx.resource}/css/bootstrap-datepicker3.min.css" rel="stylesheet">
  <title><c:choose><c:when test="${isFactoring }">新增资产包</c:when><c:when test="${!isFactoring }">添加租约</c:when></c:choose> - 租房宝金融管理平台</title>
</head>
<body>

  <%@ include file="/WEB-INF/include/header.jsp"%>
  
  <div class="web-g-main">

    <%@ include file="/WEB-INF/include/aside.jsp"%>
    <div class="content">
      <header class="header clearfix">
          <h3 class="pull-left"><c:choose><c:when test="${isFactoring }">新增资产包</c:when><c:when test="${!isFactoring }">添加租约</c:when></c:choose></h3>
       </header>
       <c:if test="${isFactoring }">
       <form id="excelfactorForm" class ="form-horizontal"  data-validate ="true" enctype="multipart/form-data">
		<div class="container">
			<div class="row">
				<div class="input-group col-sm-4 pull-left col-sm-offset-1">
					<span class="input-group-addon">导入excel文件</span>
      				<input type="file" name="file" class="form-control" id="inputGroupSuccess3" aria-describedby="inputGroupSuccess3Status">
				</div>
				<div class="input-group col-sm-4 pull-left">
				<button type="submit" class="btn btn-default btn-shadow btn-shadow-default demo2do-btn">添加</button>
				</div>
			</div>
		</div>
		</form>
		</c:if>
		<c:if test="${!isFactoring }">
		<form id="excelContractForm" class ="form-horizontal"  data-validate ="true" enctype="multipart/form-data">
		<div class="container">
			<div class="row">
				<div class="input-group pull-left col-sm-2">
		         	<span class="input-group-addon">商户</span>
		         	    <select name="app" class="form-control">
			              <option value=""></option>
			              <c:forEach var="item" items="${appList}">
			                <option value="${item.id }" <c:if test="${item.id eq app }">selected</c:if>>${item.name }</option>
			              </c:forEach>
			            </select>
		         </div>
				<div class="input-group col-sm-4 pull-left col-sm-offset-1">
					<span class="input-group-addon">导入excel文件</span>
      				<input type="file" name="file" class="form-control" id="inputGroupSuccess3" aria-describedby="inputGroupSuccess3Status">
				</div>
				<div class="input-group col-sm-4 pull-left">
				<button type="submit" class="btn btn-default btn-shadow btn-shadow-default demo2do-btn">添加</button>
				</div>
			</div>
		</div>
		</form>
		</c:if>
		<br>
       <c:if test="${not empty infoMessage}">
            <div class="alert alert-success alert-dismissable alert-fade top-margin-10 text-align-center">
              <button type="button" class="close" data-dismiss="alert">&times;</button>
              <i class="glyphicon glyphicon-info-sign"></i>&nbsp;&nbsp;
              <fmt:message key="${infoMessage}" />
            </div>
       </c:if>

       <form id="form" class ="form-horizontal"  data-validate ="true">
       <input type="hidden" value="${isFactoring }" id="isFactoring" name="isFactoring"/>
       <c:if test="${isFactoring }">
        <div class="row">
         <div class="col-xs-12">
           <div class="panel panel-default feature-panel">
           
             <div class="panel-heading">
               <h3 class="panel-title">资产包信息</h3>
             </div>
             
             <div class="panel-body">
             
               <div class="form-group demo2do-form-group">
               
                 <label class="col-xs-2 control-label">融出方</label>
                 <div class="col-xs-4">
                   <select class="form-control" name="company">
                     <c:forEach var="company"  items="${companyList}">
                       <option value="${company.id }">${company.fullName }</option>
                     </c:forEach>
                   </select>
                 </div>
                 
                 <label class="col-xs-2 control-label">保理合同编号</label>
                   <div class="col-xs-4">
             			<select class="form-control" name="factoringContractNo" id="factoringContractNo">
                           <c:forEach var="factoringContract" items="${factoringContractList }">
                             <option value=${factoringContract.id }>${factoringContract.contractNo }</option>
                           </c:forEach>
                        </select>                        
                   </div>
               </div>
               <div class="form-group demo2do-form-group">
                   <label class="col-xs-2 control-label">融资款支付金额</label>
                   <div class="col-xs-4">
                     <div class="input-group">
                       <input type="text" name="otsdAmt" class="form-control" required="required"/>
                       <span class="input-group-addon">元</span>
                     </div>
                   </div>
                   <label class="col-xs-2 control-label">融资款支付日期</label>
                   <div class="col-xs-4">
                         <input type="date" class="form-control input-date" name="latestSettleDate" required="required"/>
                   </div>
               </div>
             </div> <!-- end panel-body -->
             
           </div><!-- end panel  -->
           
         </div><!-- end col  -->
         
       </div><!-- end row  -->
       </c:if>
       
           <div class="row">
         <div class="col-xs-12">
           <div class="panel panel-default feature-panel contract">
           
             <div class="panel-heading">
               <h3 class="panel-title">租赁合同信息</h3>
             </div>
             
             <div class="panel-body">
             
               <c:if test="${!isFactoring }">
               <div class="form-group demo2do-form-group">
                 <label class="col-xs-2 control-label">服务商</label>
                 <div class="col-xs-4">
                   <select class="form-control apps" name="app" id="app">
                       <c:forEach var="app" items="${appList }">
                         <option value="${app.id }">${app.name }</option>
                       </c:forEach>
                   </select>
                 </div>               
               </div>
               </c:if>
               
               <div class="form-group demo2do-form-group">
               
                 <label class="col-xs-2 control-label">租赁合同编号</label>
                 <div class="col-xs-4">
                     <input name="contractNo" type="text" class="form-control" required ="required">
                 </div>
                 <label class="col-xs-2 control-label">租赁合同承租方</label>
                 <div class="col-xs-4">
                     <input type="text" name="customerName" class="form-control" required="required"/>
                 </div>
               </div>
               <div class="form-group demo2do-form-group">
                   <label class="col-xs-2 control-label">手机号</label>
                   <div class="col-xs-4">
                       <input type="text" name="phone" class="form-control" placeholder="可不填" />
                   </div>
              	</div>
                <%-- <c:if test="${isFactoring }"> --%>
               <div class="form-group demo2do-form-group">
                   <label class="col-xs-2 control-label">付款人</label>
                   <div class="col-xs-4">
                       <input type="text" name="payerName" class="form-control" placeholder="可不填" />
                   </div>
                   <label class="col-xs-2 control-label">付款账号</label>
                   <div class="col-xs-4">
                       <input name="payAcNo" type="text" class="form-control" placeholder="可不填" />
                   </div>
              		</div>
               <%-- </c:if> --%>
               
               <div class="form-group demo2do-form-group">
                 
                 <label class="col-xs-2 control-label">租赁周期</label>
                 <div class="col-xs-4">
                   <div class="input-group" id="rentDate">
                       <input type="date" class="input-sm form-control input-date" name="start" required="required"/>
                       <span class="input-group-addon">至</span>
                       <input type="date" class="input-sm form-control input-date" name="end"  required="required"/>
                   </div>
                 </div>
               </div>
              
               <div class="form-group demo2do-form-group">
               
                 <label class="col-xs-2 control-label">月租金</label>
                 <div class="col-xs-4">
                   <div class="input-group">
                     <input name="monthFee" type="text" class="form-control" data-validate="required">
                     <span class="input-group-addon">元/月</span>
                   </div>
                 </div>
                 
                 <label class="col-xs-2 control-label">租赁合同押金</label>
                 <div class="col-xs-4">
                   <div class="input-group">
                     <input type="text" min="0" class="form-control" name="deposit" required="required"/>
                     <span class="input-group-addon">元</span>
                   </div>
                 </div>
                 
               </div>
               
             </div> <!-- end panel-body -->
             
           </div><!-- end panel  -->
           
         </div><!-- end col  -->
         
       </div>
       
       <div class="row">
         <div class="col-xs-12">
           <div class="panel panel-default feature-panel house">
           
             <div class="panel-heading">
               <h3 class="panel-title">房源信息</h3>
             </div>
             
             <div class="panel-body">
             
               <div class="form-group demo2do-form-group">
               
                 <label class="col-xs-2 control-label">房间编号</label>
                 <div class="col-xs-4">
                   <input type="text" class="form-control" name="houseNo" data-validate="required">
                 </div>
                 
                 <label class="col-xs-2 control-label">房源地址</label>
                 <div class="col-xs-4">
                   <input type="text" class="form-control" name="address" data-validate="required">
                 </div>
                 
               </div>
               
             </div> <!-- end panel-body -->
             
           </div><!-- end panel  -->
           
         </div><!-- end col  -->
         
       </div><!-- end row  -->
       
        <div class="row">
         <div class="col-xs-12">
           <div class="panel panel-default feature-panel">
           
             <div class="panel-heading">
               <div class="row">
                 <div class="col-xs-2">
                    <h3 class="panel-title">应收款</h3>
                 </div>
                 <div class="col-xs-2 col-xs-offset-7">
                    <a class="btn btn-info gen-rent-order-table" href="javascript:void(0)" role="button">
                     <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                     生成应收款列表
                    </a>
                 </div>
               </div>
              
             </div>
             <div class="panel-body rent-order">
             
               <div class="form-group demo2do-form-group">
               
                 <label class="col-xs-2 control-label"><nobr>还款编号固定部分</nobr></label>
                 <div class="col-xs-4">
                   <input type="text" class="form-control" name="rentOrderNoFixed" data-validate="required">
                 </div>
                 
                 <label class="col-xs-2 control-label"><nobr>还款编号首期变动部分</nobr></label>
                 <div class="col-xs-4">
                   <input type="number" min="0" step="1" class="form-control" name="rentOrderNoChanged" data-validate="number">
                 </div>
                 
               </div>
               
               <div class="form-group demo2do-form-group">
               
                 <label class="col-xs-2 control-label"><nobr>首期回款-应付日期</nobr></label>
                 <div class="col-xs-4">
                   <input type="date" class="form-control" name="rentOrderFirstPayDate"  required="required"/>
                 </div>
                 
                 <label class="col-xs-2 control-label"><nobr>首期回款-应付金额</nobr></label>
                 <div class="col-xs-4 input-group">
                   <input type="text" class="form-control" name="rentOrderFirstPayAmt" data-validate="required">
                   <span class="input-group-addon">元</span>
                 </div>
                 
               </div>
               
                  <div class="form-group demo2do-form-group">
               
                 <label class="col-xs-2 control-label">应付款期数</label>
                 <div class="col-xs-4">
                   <input type="number" class="form-control"  min="1" name="rentOrderTerm" data-validate="number">
                 </div>
               </div>
               
             </div> <!-- end panel-body -->
             
           </div><!-- end panel  -->
           
         </div><!-- end col  -->
         
       </div><!-- end row  -->
       
       <div class="row text-center vertical-margin-20"> 
       
         <button type="submit" class="btn btn-success btn-shadow btn-shadow-success demo2do-btn"><c:choose><c:when test="${isFactoring }">添加资产包</c:when><c:when test="${!isFactoring }">添加租约</c:when></c:choose></button>&nbsp;&nbsp;&nbsp;&nbsp;
         <button type="button" class="btn btn-default btn-shadow btn-shadow-default demo2do-btn test">取消添加</button>
         
       </div>
       
       </form>

       <%@ include file="/WEB-INF/include/footer.jsp"%>
       
    </div>

  </div>
  

  <%@ include file="/WEB-INF/include/script.jsp"%>
  <script src="${ctx.resource}/js/bootstrap-alert-fade-trigger.js"></script>
  <script src="${ctx.resource}/js/bootstrap-datepicker.js"></script>
  <script src="${ctx.resource}/js/bootstrap-datepicker.zh-CN.js"></script>
  <script src="${ctx.resource}/js/bootstrap.validate.js"></script>
  <script src="${ctx.resource}/js/bootstrap.validate.en.js"></script>
  <script src="${ctx.resource}/js/bootstrap-alert-fade-trigger.js"></script>
  <script src="${ctx.resource}/js/bootstrap.validate.trigger.js"></script>
  <script src="${ctx.resource }/js/jquery.form.min.js"></script>
  
  <script> 
    $(document).ready(function() {

       $('.gen-rent-order-table').bind('click',function(){
    	  
    	  $('.rent-order-list').remove();
    	  var start = new Date($('input[name=start]').val());
    	  var end = new Date($('input[name=end]').val());
    	  var firstOrderNoFixed = $('input[name="rentOrderNoFixed"]').val();
    	  var firstOrderNoChanged = new Number($('input[name="rentOrderNoChanged"]').val());
    	  var firstPayDate = new Date($('input[name="rentOrderFirstPayDate"]').val());
    	  var firstPayAmt = $('input[name="rentOrderFirstPayAmt"]').val();
    	  var rentOrderTerm = $('input[name="rentOrderTerm"]').val();
    	  var isFactoring = $('input[name="isFactoring"]').val();
    	  var array = [];
    	  var start_day = start.getDate();
    	  var end_day = end.getDate();
		  for(var i = 1; i <= rentOrderTerm; i ++){
			  var month = firstPayDate.getMonth() + 1;
	      	  var year = firstPayDate.getFullYear();
	      	  var day = firstPayDate.getDate();
	      	  var end_month = month + 1;
			  if(i == 1){
				  if(isFactoring == "true"){
		              array.push('<tr><td>'+i+'</td><td><input name="orderNo" type="text" class="form-control" required ="required" value='+firstOrderNoFixed+''+(firstOrderNoChanged+i-1)+'></td><td><input type="text" class="form-control" name="orderAmount" required="required" value='+firstPayAmt+' /></td><td><input type="text" class="input-sm form-control" name="orderPayDate" value='+year+'-'+month+'-'+day+' /></td><td><input type="text" class="input-sm form-control" name="startDate" value='+year+'-'+month+'-'+start_day+' /></td><td><input type="text" class="input-sm form-control" name="endDate" value='+year+'-'+end_month+'-'+start_day+' /></td><td><select class="form-control" name="repaymentType"><c:forEach var="item"  items="${RepaymentType}"><option <c:if test="${item.ordinal() == 1}">selected="selected"</c:if> value="${item.ordinal() }"><fmt:message key="${item.key}" /></option></c:forEach></select></td></tr>')
		          }else{
		              array.push('<tr><td>'+i+'</td><td><input name="orderNo" type="text" class="form-control" required ="required" value='+firstOrderNoFixed+''+(firstOrderNoChanged+i-1)+'></td><td><input type="text" class="form-control" name="orderAmount" required="required" value='+firstPayAmt+' /></td><td><input type="text" class="input-sm form-control" name="orderPayDate" value='+year+'-'+month+'-'+day+' /></td><td><input type="text" class="input-sm form-control" name="startDate" value='+year+'-'+month+'-'+start_day+' /></td><td><input type="text" class="input-sm form-control" name="endDate" value='+year+'-'+end_month+'-'+start_day+' /></td></tr>')
		          }
			  }else{
				  var nextDate = addMonth(firstPayDate,i);
		          month = nextDate.getMonth() + 1;
		          year = nextDate.getFullYear();
		          day = nextDate.getDate();
		          var end_month = month + 1;
		          if(month == 0){
		            month = 12;
		            year = year - 1;
		          }
		          if(isFactoring == "true"){
		              array.push('<tr><td>'+i+'</td><td><input name="orderNo" type="text" class="form-control" required ="required" value='+firstOrderNoFixed+''+(firstOrderNoChanged+i-1)+'></td><td><input type="text" class="form-control" name="orderAmount" required="required" value='+firstPayAmt+' /></td><td><input type="text" class="input-sm form-control" name="orderPayDate" value='+year+'-'+month+'-'+day+' /></td><td><input type="text" class="input-sm form-control" name="startDate" value='+year+'-'+month+'-'+start_day+' /></td><td><input type="text" class="input-sm form-control" name="endDate" value='+year+'-'+end_month+'-'+start_day+' /></td><td><select class="form-control" name="repaymentType"><c:forEach var="item"  items="${RepaymentType}"><option <c:if test="${item.ordinal() == 0}">selected="selected"</c:if> value="${item.ordinal() }"><fmt:message key="${item.key}" /></option></c:forEach></select></td></tr>')
		          }else{
		              array.push('<tr><td>'+i+'</td><td><input name="orderNo" type="text" class="form-control" required ="required" value='+firstOrderNoFixed+''+(firstOrderNoChanged+i-1)+'></td><td><input type="text" class="form-control" name="orderAmount" required="required" value='+firstPayAmt+' /></td><td><input type="text" class="input-sm form-control" name="orderPayDate" value='+year+'-'+month+'-'+day+' /></td><td><input type="text" class="input-sm form-control" name="startDate" value='+year+'-'+month+'-'+start_day+' /></td><td><input type="text" class="input-sm form-control" name="endDate" value='+year+'-'+end_month+'-'+start_day+' /></td></tr>')
		          }
			  }
		  }
		  
		  if(isFactoring == "true"){
			  var rentOrderTable = '<div class="rent-order-list">'+
	          '<table class="table table-striped demo2do-table bottom-margin-20">'+
			          '<thead><tr><th>序号</th><th>单笔回款-还款编号</th><th>单笔回款-应付金额</th><th>单笔回款-应付日期</th><th>起始日期</th><th>结束日期</th><th>回款处理方式<th/></tr></thead>'+
			          '<tbody>'+array.join("")+'</tbody>'+
			    '</table>'+'</div>';			  
		  }else{
	    	  var rentOrderTable = '<div class="rent-order-list">'+
	          '<table class="table table-striped demo2do-table bottom-margin-20">'+
			          '<thead><tr><th>序号</th><th>单笔回款-还款编号</th><th>单笔回款-应付金额</th><th>单笔回款-应付日期</th><th>起始日期</th><th>结束日期</th></tr></thead>'+
			          '<tbody>'+array.join("")+'</tbody>'+
			    '</table>'+'</div>';			  
		  }

    	  $('.rent-order').append(rentOrderTable); 
    	  
      });
       
       var addMonth = function (date,m)
       {
    	   var tempDate = new Date(date.getFullYear(),date.getMonth(),date.getDate());
         var tempday = tempDate.getDate();
         tempDate.setMonth(tempDate.getMonth()+m-1);
         if (tempday != tempDate.getDate()){
        	 tempDate.setDate(0);
         }
         return tempDate;
       }

      
      $("#form").ajaxForm({
    	  url:'${ctx}/assets-package/add',
    	  type:'POST',
   		  success:function(data){
   			var jsonData=$.parseJSON(data);
       		  $('#dialog').find('.modal-body p').text(jsonData.message).end().modal();
       	  },
       	  error:function(XmlHttpRequest,textStatus, errorThrown)
       	  {
       	  alert("保存失败;"+XmlHttpRequest.responseText);
       	  }
      });
      $("#excelfactorForm").ajaxForm({
    	  url:'${ctx}/assets-package/excel-create-assetData',
    	  type:'POST',
   		  success:function(data){
   			alert(data);
       	  }
      });
      $("#excelContractForm").ajaxForm({
    	  url:'${ctx}/contracts/import-contract-excel',
    	  type:'POST',
   		  success:function(data){
   			alert(data);
       	  }
      });
    });
  </script>
<!--   <script>
	  $("#factoringContractNo").bind("change", function(){
		  var contract_selected = $("#factoringContractNo").find("option:selected").val();
		  $("#app").val(contract_selected); 
	  });
  </script> -->
  <!-- Modal -->
<div class="modal fade" id="dialog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">提示</h4>
      </div>
      <div class="modal-body">
        <p></p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
</body>
</html>